//
//  ViewControllerForVideoAndRadio.m
//  世界
//
//  Created by admin on 16/2/14.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ViewControllerForVideoAndRadio.h"
#import "CellForVideo.h"
#import "CellForRadio.h"
#import "ViewControllerForVideoPlay.h"
#import "ViewControllerForVideoList.h"
#import "ViewControllerForRadioList.h"
#import "ViewControllerForRadioPlay.h"


@interface ViewControllerForVideoAndRadio ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *collection;

@end

@implementation ViewControllerForVideoAndRadio

#pragma mark 重新dealloc方法
- (void)dealloc
{
    [super dealloc];
    
    [_flowLayout release];
    
    [_collection release];
    
    [_seg release];
}




#pragma mark 视图的生命周期 >>>>>>
#pragma mark 视图加载完成
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    [self addSegmentedControl];
    
    [self createCollectionView];
    
    [self createKVO];
    
    [self createObserver];
}

#pragma mark 视图即将显示
- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh" object:nil];
}



#pragma mark 接受到内存警告!!!
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



#pragma mark 自定义方法 >>>>>>
#pragma mark 创建UICollectionViewFlowLayout
- (void)createFlowLayout
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //简单布局API:
    self.flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height-64-49);
    
    //item之间的最小间距(纵向)
    self.flowLayout.minimumLineSpacing = 0;
    
    //滑动方向 (水平)
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}





#pragma mark 创建UICollectionView
- (void)createCollectionView
{
    [self createFlowLayout];
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:self.flowLayout];
    
    [_flowLayout release];
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    //不显示水平方向的滑动条
    self.collection.showsHorizontalScrollIndicator = NO;
    
    //开启翻页效果
    self.collection.pagingEnabled = YES;
    
    [self.view addSubview: self.collection];
    
    [_collection release];
    
    
    //指定代理人
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    //注册视频列表的Cell类型 (Cell 只支持先注册)
    [self.collection registerClass:[CellForVideo class] forCellWithReuseIdentifier:@"pool1"];
    
    //注册电台列表的Cell类型 (Cell 只支持先注册)
    [self.collection registerClass:[CellForRadio class] forCellWithReuseIdentifier:@"pool2"];
}




#pragma mark UICollectionViewDataSource的协议方法 >>>>>>
#pragma mark 返回Item的个数 (required 1/2)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}




#pragma mark 给每个Item山添加Cell (required 2/2)
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    /**
     * 第一个Item上添加视频类的Cell
     * 第二个Item上添加电台类的Cell
     */
    if (indexPath.item == 0)
    {
        CellForVideo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
        
        return cell;
    }
    else
    {
        CellForRadio *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool2" forIndexPath:indexPath];
        
        return cell;
    }
}




#pragma mark 添加分段控制
- (void)addSegmentedControl
{
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"视频", @"电台"]];
    
    self.navigationItem.titleView = self.seg;
    
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    [_seg release];
}




#pragma mark 点击分段控制所触发的方法
- (void)segAction:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex)
    {
        case 0:
            
            self.collection.contentOffset = CGPointMake(0, -64);
            
            break;
            
        case 1:
            
            self.collection.contentOffset = CGPointMake(414, -64);
            
            break;
    }
}


#pragma mark 添加键值观察者
- (void)createKVO
{
    //观察collection的contentOffset的属性
    [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"context"];
}




#pragma mark 当被观察值属性发生变换的时候调用此方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    //collection的contentOffset的属性发生变化后的X的值
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    
    //为0时
    if (x == self.view.frame.origin.x)
    {
        //分段控制器选择视频项
        self.seg.selectedSegmentIndex = 0;
    }
    
    //为屏幕的宽时
    if (x == self.view.frame.size.width)
    {
        //分段控制选择电台项
        self.seg.selectedSegmentIndex = 1;
    }
}





#pragma mark 添加观察者
- (void)createObserver
{
    
   
    
    
    
    
     //推出指定类型视频目录界面
    [[NSNotificationCenter defaultCenter] addObserverForName:@"Video1" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        ViewControllerForVideoList *videoList = [[ViewControllerForVideoList alloc] init];
        
        //设置标题为所选项目
        videoList.navigationItem.title = [note.userInfo objectForKey:@"title"];
        
        videoList.strForPresentItem = [note.userInfo objectForKey:@"item"];
        
        //1.设置self.tabBarController.tabBar.hidden=YES;
        self.tabBarController.tabBar.hidden = YES;
        
        //2.如果在push跳转时需要隐藏tabBar，设置self.hidesBottomBarWhenPushed=YES;
        self.hidesBottomBarWhenPushed=YES;
        
        [self.navigationController pushViewController:videoList animated:YES];
        
        //3.在push后设置self.hidesBottomBarWhenPushed=NO,这样back回来的时候，tabBar会恢复正常显示。
        self.hidesBottomBarWhenPushed=NO;
    }];
    
    
    
    //推出视频播放界面
    [[NSNotificationCenter defaultCenter] addObserverForName:@"Video" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        ViewControllerForVideoPlay *video = [[ViewControllerForVideoPlay alloc] init];
        
        video.mp4_url = [note.userInfo objectForKey:@"item"];
        
        video.vid = [note.userInfo objectForKey:@"vid"];
        
        video.lableForTitle.text = [note.userInfo objectForKey:@"title"];
       
        
        [self presentViewController:video animated:YES completion:^{
            
            video.lableForTitle.text = [note.userInfo objectForKey:@"title"];
            
        }];
        
    }];
    
    
    //推出电台播放列表
    [[NSNotificationCenter defaultCenter] addObserverForName:@"Radio" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        ViewControllerForRadioList *radioList = [[ViewControllerForRadioList alloc] init];
        
        radioList.strForURL = [note.userInfo objectForKey:@"strForURL"];
        
        //设置标题为所选项目
        radioList.navigationItem.title = [note.userInfo objectForKey:@"Cname"];
        
        //1.设置self.tabBarController.tabBar.hidden=YES;
        self.tabBarController.tabBar.hidden = YES;
        
        //2.如果在push跳转时需要隐藏tabBar，设置self.hidesBottomBarWhenPushed=YES;
        self.hidesBottomBarWhenPushed=YES;

        [self.navigationController pushViewController:radioList animated:YES];

        
        //3.在push后设置self.hidesBottomBarWhenPushed=NO,这样back回来的时候，tabBar会恢复正常显示。
        self.hidesBottomBarWhenPushed=NO;
    }];
    
    //推出电台播放器
    [[NSNotificationCenter defaultCenter] addObserverForName:@"RadioPlay" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        ViewControllerForRadioPlay *radioPlay = [[ViewControllerForRadioPlay alloc] init];
        
        radioPlay.strForURL = [note.userInfo objectForKey:@"postid"];
        
        
        NSString *str  = [note.userInfo objectForKey:@"number"];
        
        NSInteger number = str.integerValue;
        
        radioPlay.number = number;
        
        radioPlay.strForTid = [note.userInfo objectForKey:@"tid"];
        
        [self presentViewController:radioPlay animated:YES completion:^{
            
            
        }];
        
        radioPlay.lableForTitle.text = [note.userInfo objectForKey:@"tname"];
        
        
    }];
}

@end
